# 文件的读写
# 读文件
f = open("./data/theZenOfPython", mode='r', encoding="utf-8")
# 读取10个字符
print(f.read(10))
# 读取1行
print(f.readline())
# 读取所有行，并将每一行作为list中的一个元素，最终返回一个list
print(f.readlines())
f.close()

# 写文件
fw = open("./data/newFile", mode="w")
fw.write("zhe shi yi hang shu jv\n")
fw.writelines(["a", "b", "c"])
fw.close()

# 读取theZenOfPython并写入newFile2
f2 = open("./data/theZenOfPython", mode='r', encoding="utf-8")
fw2 = open("./data/newFile2", mode="w")
# 直接读取所有数据并以list形式返回，当文件比较小 可以这样做
# for l in f2.readlines():
#     fw2.write(l)
line = f2.readline()
while line != "":
    fw2.write(line)
    # 读取下一行
    # 当没有下一行数据时 返回 空字符串
    line = f2.readline()

fw2.close()
f2.close()

# with open
# 实际上这种方式可以省略close()
with open("./data/theZenOfPython", mode='r', encoding="utf-8") as f3:
    with open("./data/newFile3", mode="a") as fw3:
        line = f3.readline()
        while line != "":
            fw3.write(line)
            # 读取下一行
            # 当没有下一行数据时 返回 空字符串
            line = f3.readline()